/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart.connection;

import java.util.List;

import org.eclipse.gef.EditPart;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.StereotypeDisplayEditPart;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Edit part represented by a graph edge that representes a link (ex: PackageImport).
 * The tag is shown in the StereotypeEditPart, which is suclassed by TagDisplayEditPart.
 */
public class TaggedGraphEdgeEditPart extends StereotypedGraphEdgeEditPart {

	/**
	 * tag for the link.
	 */
//	final protected String tag;
	protected String tag;

	/**
	 * Creates a new TaggedGraphEdgeEditPart.
	 * 
	 * @param graphEdge 
	 * @param modelManager 
	 * @param tag 
	 */
	public TaggedGraphEdgeEditPart(GraphEdge graphEdge, ModelManager modelManager, String tag) {
		super(graphEdge, modelManager);
		this.tag = tag;
	}

	/**
	 * Creates a new TaggedGraphEdgeEditPart, with no tag to be compliant with
	 * only name edit part with no tag.
	 * 
	 * @param graphEdge 
	 * @param modelManager 
	 * @param tag 	 */
	public TaggedGraphEdgeEditPart(GraphEdge graphEdge, ModelManager modelManager) {
		super(graphEdge, modelManager);
		this.tag = null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createChild(java.lang.Object)
	 */
	/**
	 * 
	 * 
	 * @param model 
	 * 
	 * @return 
	 */
	@Override
	protected EditPart createChild(Object model) {
		if( model instanceof GraphNode) {
			// graphNode n�0 = name of the element if it is a named editor part
			// graphNode n�1 = stereotypes applied to the element or index = 0 if no name

			List<GraphNode> childrens = getModelChildren(); 
			GraphNode element = null;
			if(childrens.size() > 1) {
				element = childrens.get(1);
			} else {
				element = childrens.get(0);
			}
			
			if(element != null) {
				if(element instanceof GraphNode && (((GraphNode)model) == element) ) {
					if(tag == null) {
						stereotypeDisplayEditPart = new StereotypeDisplayEditPart(
								element,
								this.getUmlElement(),
								modelManager,
								this);
					} else {
						stereotypeDisplayEditPart = new TagDisplayEditPart(
								element,
								this.getUmlElement(),
								modelManager,
								this,
								tag);
					}
					return stereotypeDisplayEditPart;
				}
			}
		}
		return super.createChild(model);
	}

}
